home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / PROFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  14.5 KB  |  590 lines

  1. #include "global.h"
  2. #include "commands.h"
  3. #include "mailbox.h"
  4. #include "files.h"
  5.  
  6. extern void usercvt (void);
  7.  
  8. static int dombnewprofile (int argc, char *argv[], void *p);
  9. static int dombdelprofile (int argc, char *argv[], void *p);
  10. static int dombchgprofile (int argc, char *argv[], void *p);
  11. static int domblistprofile (int argc, char *argv[], void *p);
  12.  
  13. static int dopasswd (int argc, char *argv[], void *p);
  14. static int donewprofile (int argc, char *argv[], void *p);
  15. static int dodelprofile (int argc, char *argv[], void *p);
  16. static int dochgprofile (int argc, char *argv[], void *p);
  17. static int dolistprofile (int argc, char *argv[], void *p);
  18. static int doprofileext (int argc, char *argv[], void *p);
  19. static int doprofileusermod (int argc, char *argv[], void *p);
  20.  
  21. static const char *profileExt = NULLCHAR;
  22. static int profileUsermod = 0;
  23.  
  24.  
  25. /* profile subcommand table */
  26. static struct cmds ProTab[] = {
  27.     { "add",        donewprofile,        0, 3, "profile add <name> (<level> | <path> <perms>)" },
  28.     { "change",        dochgprofile,        0, 3, "profile change <name> (<level> | <path> <perms>)" },
  29.     { "delete",        dodelprofile,        0, 2, "profile delete <name>" },
  30.     { "fileext",        doprofileext,        0, 0, NULLCHAR },
  31.     { "list",        dolistprofile,        0, 2, "profile list <name>" },
  32.     { "passwd",        dopasswd,        0, 2, "profile passwd <username>" },
  33.     { "usermodifyable",    doprofileusermod,    0, 0, NULLCHAR },
  34.     { NULLCHAR,        NULL,            0, 0, NULLCHAR }
  35. };
  36.  
  37.  
  38.  
  39. int
  40. doprofile (int argc, char *argv[], void *p)
  41. {
  42.     return subcmd (ProTab, argc, argv, p);
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49. static int
  50. dopasswd (int argc, char *argv[], void *p OPTIONAL)
  51. {
  52. struct mbx m;
  53.  
  54.     m.privs = SYSOP_CMD;
  55.     m.quickfile = (FILE *) -1;
  56.     m.authenticated = 1;
  57.     m.sid = 0;
  58.     m.user = Curproc->output;
  59.     m.mfile = NULLFILE;
  60.     strcpy (m.name, "sysop");
  61.     (void) dombpasswd (argc, argv, &m);
  62.     return 0;
  63. }
  64.  
  65.  
  66.  
  67. static int
  68. doprofileusermod (int argc, char *argv[], void *p OPTIONAL)
  69. {
  70.     return setbool (&profileUsermod, "Allow users to modify their passwords in PBBS", argc, argv);
  71. }
  72.  
  73.  
  74.  
  75.  
  76. static int
  77. doprofileext (int argc, char *argv[], void *p OPTIONAL)
  78. {
  79. char *cp;
  80.  
  81.     if (argc < 2)    {
  82.         if (profileExt != NULLCHAR)
  83.             tprintf ("%s\n", profileExt);
  84.     } else    {
  85.         free (profileExt);
  86.         profileExt = NULLCHAR;
  87.         cp = skipwhite (argv[1]);
  88.         if (*cp)    {
  89.             if (*cp == '.')
  90.                 cp++;
  91.             if (*cp)
  92.                 profileExt = strdup (cp);
  93.         }
  94.     }
  95.     return 0;
  96. }
  97.  
  98.  
  99.  
  100. static char *
  101. find_entry (FILE *fp, FILE *fpout, char *buf, int buflen, char *searchfor)
  102. {
  103. int foundit = 0;
  104. char c;
  105. char *cp = NULLCHAR;
  106.  
  107.     while (fgets (buf, buflen, fp) != NULLCHAR) {
  108.         kwait (NULL);
  109.         rip (buf);
  110.         if (*buf && *buf != '#')    {
  111.             cp = strpbrk (buf, " \t");
  112.             if (cp != NULLCHAR)    {
  113.                 c = *cp;
  114.                 *cp++ = 0;
  115.                 if (!strcasecmp (searchfor, buf))    {
  116.                     foundit = 1;
  117.                     break;
  118.                 }
  119.                 *(--cp) = c;
  120.             }
  121.         }
  122.         if (fpout != NULLFILE)
  123.             fprintf (fpout, "%s\n", buf);
  124.     }
  125.     return ((foundit) ? cp : NULLCHAR);
  126. }
  127.  
  128.  
  129.  
  130. static void
  131. add_rest_of_file (FILE *fp, FILE *fpout, char *buf, int buflen)
  132. {
  133.     /* now add the rest of the file */
  134.     while (fgets (buf, buflen, fp) != NULLCHAR) {
  135.         kwait (NULL);
  136.         fputs (buf, fpout);
  137.     }
  138. }
  139.  
  140.  
  141.  
  142. int
  143. dombpasswd (int argc, char *argv[], void *p)
  144. {
  145. struct mbx *m;
  146. char buf[MBXLINE], *cp = NULLCHAR;
  147. char usrfilename[MBXLINE];
  148. char tmpfilename[MBXLINE];
  149. FILE *fp, *fpout;
  150. int foundit = 0, changedit = 0;
  151. char *searchfor;
  152. char firstpass[60];
  153.  
  154.     m = (struct mbx *) p;
  155.     if (argc > 1 && !(m->privs & SYSOP_CMD)) {
  156.         /* only a sysop can change other users' passwords */
  157.         tputs ("Sorry, but you do not have permission to change others' passwords.\n");
  158.         tputs ("Usage: passwd\n");
  159.         return 1;
  160.     }
  161.  
  162.     if (m->privs & WAS_ANONY)    {
  163.         /* anonymous users don't have defined passwords */
  164.         tputs ("Sorry, but anonymous users are not allowed to save/change passwords.\n");
  165.         return 1;
  166.     }
  167.  
  168.     if (!m->authenticated)    {
  169.         /* user has not already authenticated themself */
  170.         tputs ("Sorry, but you have not been authenticated for this login. You must be connect via AX25.\nSorry, but you are not allowed to change your password at this time.\n");
  171.         return 1;
  172.     }
  173.     
  174.     if (!profileUsermod && !(m->privs & SYSOP_CMD))    {
  175.         /* disabled feature - by sysop */
  176.         tputs ("Sorry, but changing passwords is disabled by the sysop.\n");
  177.         return 1;
  178.     }
  179.  
  180.     if (argc > 1)
  181.         searchfor = argv[1];
  182.     else
  183.         searchfor = m->name;
  184.  
  185.     sprintf (usrfilename, "%s%s%s", Userfile, (profileExt) ? "." : "",
  186.         (profileExt) ? profileExt : "");
  187.     if ((fp = fopen (usrfilename, READ_TEXT)) == NULLFILE)    {
  188.         tputs ("Sorry, but there is a problem at this site. Can't change password!\n");
  189.         return 1;
  190.     }
  191.  
  192.     sprintf (tmpfilename, "%s.tmp", Userfile);
  193.     if ((fpout  = fopen (tmpfilename, WRITE_TEXT)) == NULLFILE)    {
  194.         tputs ("Sorry, but there is a problem at this site. Can't change password!\n");
  195.         return 1;
  196.     }
  197.  
  198.     cp = find_entry (fp, fpout, buf, MBXLINE, searchfor);
  199.     if (cp != NULLCHAR)    {
  200.         foundit = 1;
  201.         cp = skipwhite (cp);    /* any leading white space */
  202.         cp = skipnonwhite (cp);    /* skip the old password */
  203.         /* cp now points to path/permission or security level */
  204.         
  205.         tprintf ("Enter new password:\n");
  206.  
  207.         m->line[0] = 0;
  208.         if (m->quickfile != NULLFILE)    {    /* from command session */
  209.             if (recvline (Curproc->input, (unsigned char *) m->line, MBXLINE) == 0)
  210.                 return 0;
  211.         } else {
  212.             if (mbxrecvline (m) == -1)
  213.                 return 0;
  214.         }
  215.  
  216.         rip (m->line);
  217.         strncpy (firstpass, m->line, 60);
  218.  
  219.         tprintf ("Enter new password again:\n");
  220.  
  221.         m->line[0] = 0;
  222.         if (m->quickfile != NULLFILE)    {    /* from command session */
  223.             if (recvline (Curproc->input, (unsigned char *) m->line, MBXLINE) == 0)
  224.                 return 0;
  225.         } else {
  226.             if (mbxrecvline (m) == -1)
  227.                 return 0;
  228.         }
  229.  
  230.         rip (m->line);
  231.  
  232.         if (!strcmp (firstpass, m->line))    {
  233.             fprintf (fpout, "%s %s %s\n", searchfor, firstpass, cp);
  234.             changedit = 1;
  235.             add_rest_of_file (fp, fpout, buf, MBXLINE);
  236.         }
  237.  
  238.     }
  239.     
  240.     (void) fclose (fp);
  241.     (void) fclose (fpout);
  242.  
  243.     if (changedit)    {
  244.         unlink (usrfilename);
  245.         (void) rename (tmpfilename, usrfilename);
  246.         tcmdprintf ("Password changed for user '%s' by '%s'\n", searchfor, m->name);
  247.         usercvt ();
  248.         tputs ("Password changed successfully");
  249.         if (argc > 1)
  250.             tprintf (" for security profile '%s'", searchfor);
  251.         tputc ('\n');
  252.     } else     {
  253.         unlink (tmpfilename);
  254.         if (foundit)
  255.             tputs ("Sorry, but those two passwords are different. Password change aborted!\n");
  256.         else
  257.             tprintf ("Sorry, but user '%s' not found in '%s'\n", searchfor, usrfilename);
  258.     }
  259.     
  260.     return 0;
  261. }
  262.  
  263.  
  264.  
  265. static int
  266. donewprofile (int argc, char *argv[], void *p OPTIONAL)
  267. {
  268. struct mbx m;
  269.  
  270.     m.privs = SYSOP_CMD;
  271.     m.quickfile = (FILE *) -1;
  272.     m.authenticated = 1;
  273.     m.sid = 0;
  274.     m.user = Curproc->output;
  275.     m.mfile = NULLFILE;
  276.     strcpy (m.name, "sysop");
  277.     (void) dombnewprofile (argc, argv, &m);
  278.     return 0;
  279. }
  280.  
  281.  
  282.  
  283. static int
  284. dochgprofile (int argc, char *argv[], void *p OPTIONAL)
  285. {
  286. struct mbx m;
  287.  
  288.     m.privs = SYSOP_CMD;
  289.     m.quickfile = (FILE *) -1;
  290.     m.authenticated = 1;
  291.     m.sid = 0;
  292.     m.user = Curproc->output;
  293.     m.mfile = NULLFILE;
  294.     strcpy (m.name, "sysop");
  295.     (void) dombchgprofile (argc, argv, &m);
  296.     return 0;
  297. }
  298.  
  299.  
  300.  
  301.  
  302. static int
  303. dodelprofile (int argc, char *argv[], void *p OPTIONAL)
  304. {
  305. struct mbx m;
  306.  
  307.     m.privs = SYSOP_CMD;
  308.     m.quickfile = (FILE *) -1;
  309.     m.authenticated = 1;
  310.     m.sid = 0;
  311.     m.user = Curproc->output;
  312.     m.mfile = NULLFILE;
  313.     strcpy (m.name, "sysop");
  314.     (void) dombdelprofile (argc, argv, &m);
  315.     return 0;
  316. }
  317.  
  318.  
  319.  
  320.  
  321. static int
  322. dolistprofile (int argc, char *argv[], void *p OPTIONAL)
  323. {
  324. struct mbx m;
  325.  
  326.     m.privs = SYSOP_CMD;
  327.     m.quickfile = (FILE *) -1;
  328.     m.authenticated = 1;
  329.     m.sid = 0;
  330.     m.user = Curproc->output;
  331.     m.mfile = NULLFILE;
  332.     strcpy (m.name, "sysop");
  333.     (void) domblistprofile (argc, argv, &m);
  334.     return 0;
  335. }
  336.  
  337.  
  338.  
  339.  
  340. /* profile subcommand table for PBBS */
  341. static struct cmds MBProTab[] = {
  342.     { "add",        dombnewprofile,        0, 3, "profile add <name> (<level> | <path> <perms>)" },
  343.     { "change",        dombchgprofile,        0, 3, "profile change <name> (<level> | <path> <perms>)" },
  344.     { "delete",        dombdelprofile,        0, 2, "profile delete <name>" },
  345.     { "list",        domblistprofile,    0, 2, "profile list <name>" },
  346.     { NULLCHAR,        NULL,            0, 0, NULLCHAR }
  347. };
  348.  
  349.  
  350.  
  351. int
  352. dombprofile (int argc, char *argv[], void *p)
  353. {
  354.     return subcmd (MBProTab, argc, argv, p);
  355. }
  356.  
  357.  
  358.  
  359.  
  360.  
  361. int
  362. dombnewprofile (int argc, char *argv[], void *p)
  363. {
  364. struct mbx *m;
  365. FILE *fp;
  366. int level = 1;
  367. char usrfilename[MBXLINE];
  368. char buf[MBXLINE], *cp;
  369.  
  370.     m = (struct mbx *) p;
  371.     if (!(m->privs & SYSOP_CMD)) {
  372.         /* only a sysop can change other users' passwords */
  373.         tputs ("Sorry, but you do not have permission to add a security profile!\n");
  374.         return 1;
  375.     }
  376.  
  377.     if (!m->authenticated)    {
  378.         /* user has not already authenticated themself */
  379.         tputs ("Sorry, but you have not been authenticated for this login. You must be connect via AX25.\nSorry, but you are not allowed to add a user profile at this time.\n");
  380.         return 1;
  381.     }
  382.     
  383.     /* first check to see if this profile already exists */
  384.     sprintf (usrfilename, "%s%s%s", Userfile, (profileExt) ? "." : "",
  385.         (profileExt) ? profileExt : "");
  386.     if ((fp = fopen (usrfilename, READ_TEXT)) == NULLFILE)    {
  387.         tprintf ("Cannot open '%s'!\n", usrfilename);
  388.         return 1;
  389.     }
  390.  
  391.     cp = find_entry (fp, NULLFILE, buf, MBXLINE, argv[1]);
  392.     (void) fclose (fp);
  393.     if (cp != NULLCHAR)    {
  394.         tprintf ("Security profile for '%s' already exists! Add command ignored!\n", argv[1]);
  395.         return 1;
  396.     }
  397.     
  398.  
  399.     /* since it doesn't exist, open to append it */
  400.     if ((fp = fopen (usrfilename, APPEND_TEXT)) == NULLFILE)    {
  401.         tprintf ("Cannot open '%s'!\n", usrfilename);
  402.         return 1;
  403.     }
  404.  
  405.     if (argc > 3)
  406.         level = 0;
  407.  
  408.     fprintf (fp, "%s new %s%s %s\n", argv[1], (level) ? "#" : "",
  409.         argv[2], (level) ? "" : argv[3]);
  410.     (void) fclose (fp);
  411.  
  412.     (void) dombpasswd (2, argv, m);
  413.     tprintf ("New security profile for '%s' added successfully\n", argv[1]);
  414.     return 0;
  415. }
  416.  
  417.  
  418.  
  419. int
  420. dombchgprofile (int argc, char *argv[], void *p)
  421. {
  422. struct mbx *m;
  423. FILE *fp, *fpout;
  424. char buf[MBXLINE];
  425. char usrfilename[MBXLINE];
  426. char tmpfilename[MBXLINE];
  427. int level = 1;
  428. char *cp, *cp2;
  429. int foundit = 0;
  430.  
  431.     m = (struct mbx *) p;
  432.     if (!(m->privs & SYSOP_CMD)) {
  433.         /* only a sysop can change other users' passwords */
  434.         tputs ("Sorry, but you do not have permission to change a security profile!\n");
  435.         return 1;
  436.     }
  437.  
  438.     if (!m->authenticated)    {
  439.         /* user has not already authenticated themself */
  440.         tputs ("Sorry, but you have not been authenticated for this login. You must be connect via AX25.\nSorry, but you are not allowed to change a user profile at this time.\n");
  441.         return 1;
  442.     }
  443.     
  444.     sprintf (usrfilename, "%s%s%s", Userfile, (profileExt) ? "." : "",
  445.         (profileExt) ? profileExt : "");
  446.     if ((fp = fopen (usrfilename, READ_TEXT)) == NULLFILE)    {
  447.         tprintf ("Cannot open '%s'!\n", usrfilename);
  448.         return 1;
  449.     }
  450.  
  451.     sprintf (tmpfilename, "%s.tmp", Userfile);
  452.     if ((fpout  = fopen (tmpfilename, WRITE_TEXT)) == NULLFILE)    {
  453.         tprintf ("Cannot create '%s'!\n", tmpfilename);
  454.         return 1;
  455.     }
  456.  
  457.     if (argc > 3)
  458.         level = 0;
  459.  
  460.     cp = find_entry (fp, fpout, buf, MBXLINE, argv[1]);
  461.     if (cp != NULLCHAR)    {
  462.         foundit = 1;
  463.         cp = skipwhite (cp);    /* any leading white space */
  464.         /* cp now points to password field */
  465.         cp2 = skipnonwhite (cp);
  466.         if (cp2)
  467.             *cp2 = 0;
  468.  
  469.         fprintf (fpout, "%s %s %s%s %s\n", argv[1], cp, (level) ? "#" : "",
  470.             argv[2], (level) ? "" : argv[3]);
  471.         add_rest_of_file (fp, fpout, buf, MBXLINE);
  472.     }
  473.  
  474.     (void) fclose (fp);
  475.     (void) fclose (fpout);
  476.  
  477.     if (foundit)    {
  478.         unlink (usrfilename);
  479.         (void) rename (tmpfilename, usrfilename);
  480.         tprintf ("Security profile for '%s' changed successfully\n", argv[1]);
  481.     } else    {
  482.         unlink (tmpfilename);
  483.         tprintf ("User '%s' not found in '%s'\n", argv[1], usrfilename);
  484.     }
  485.     return 0;
  486. }
  487.  
  488.  
  489. int
  490. domblistprofile (int argc OPTIONAL, char *argv[], void *p)
  491. {
  492. struct mbx *m;
  493. FILE *fp;
  494. char buf[MBXLINE];
  495. char usrfilename[MBXLINE];
  496. char *cp;
  497.  
  498.     m = (struct mbx *) p;
  499.     if (!(m->privs & SYSOP_CMD)) {
  500.         /* only a sysop can change other users' passwords */
  501.         tputs ("Sorry, but you do not have permission to list a security profile!\n");
  502.         return 1;
  503.     }
  504.  
  505.     if (!m->authenticated)    {
  506.         /* user has not already authenticated themself */
  507.         tputs ("Sorry, but you have not been authenticated for this login. You must be connect via AX25.\nSorry, but you are not allowed to list a user profile at this time.\n");
  508.         return 1;
  509.     }
  510.     
  511.     sprintf (usrfilename, "%s%s%s", Userfile, (profileExt) ? "." : "",
  512.         (profileExt) ? profileExt : "");
  513.     if ((fp = fopen (usrfilename, READ_TEXT)) == NULLFILE)    {
  514.         tprintf ("Cannot open '%s'!\n", usrfilename);
  515.         return 1;
  516.     }
  517.  
  518.     cp = find_entry (fp, NULLFILE, buf, MBXLINE, argv[1]);
  519.     (void) fclose (fp);
  520.     if (cp != NULLCHAR)    {
  521.         cp = skipwhite (cp);    /* any leading white space */
  522.         cp = skipnonwhite (cp);
  523.         /* cp now points to path/permission or security level */
  524.  
  525.         tprintf ("Security profile for '%s': %s\n", argv[1], cp);
  526.     } else
  527.         tprintf ("User '%s' not found in '%s'\n", argv[1], usrfilename);
  528.     return 0;
  529. }
  530.  
  531.  
  532.  
  533. int
  534. dombdelprofile (int argc OPTIONAL, char *argv[], void *p)
  535. {
  536. struct mbx *m;
  537. FILE *fp, *fpout;
  538. char buf[MBXLINE];
  539. char usrfilename[MBXLINE];
  540. char tmpfilename[MBXLINE];
  541. char *cp;
  542.  
  543.     m = (struct mbx *) p;
  544.     if (!(m->privs & SYSOP_CMD)) {
  545.         /* only a sysop can change other users' passwords */
  546.         tputs ("Sorry, but you do not have permission to delete a security profile!\n");
  547.         return 1;
  548.     }
  549.  
  550.     if (!m->authenticated)    {
  551.         /* user has not already authenticated themself */
  552.         tputs ("Sorry, but you have not been authenticated for this login. You must be connect via AX25.\nSorry, but you are not allowed to delete a user profile at this time.\n");
  553.         return 1;
  554.     }
  555.     
  556.     sprintf (usrfilename, "%s%s%s", Userfile, (profileExt) ? "." : "",
  557.         (profileExt) ? profileExt : "");
  558.     if ((fp = fopen (usrfilename, READ_TEXT)) == NULLFILE)    {
  559.         tprintf ("Cannot open '%s'!\n", usrfilename);
  560.         return 1;
  561.     }
  562.  
  563.     sprintf (tmpfilename, "%s.tmp", Userfile);
  564.     if ((fpout  = fopen (tmpfilename, WRITE_TEXT)) == NULLFILE)    {
  565.         tprintf ("Cannot create '%s'!\n", tmpfilename);
  566.         return 1;
  567.     }
  568.  
  569.     cp = find_entry (fp, fpout, buf, MBXLINE, argv[1]);
  570.  
  571.     if (cp != NULLCHAR)    {
  572.         add_rest_of_file (fp, fpout, buf, MBXLINE);
  573.  
  574.         (void) fclose (fp);
  575.         (void) fclose (fpout);
  576.         unlink (usrfilename);
  577.         (void) rename (tmpfilename, usrfilename);
  578.         tprintf ("Security profile for '%s' deleted successfully\n", argv[1]);
  579.     } else    {
  580.         (void) fclose (fp);
  581.         (void) fclose (fpout);
  582.         unlink (tmpfilename);
  583.         tprintf ("User '%s' not found in '%s'\n", argv[1], usrfilename);
  584.     }
  585.     return 0;
  586. }
  587.  
  588.  
  589.  
  590.